using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;

public class YizizhuiyaoAnalysis : BatchProcessor
{
    //private static string shoubanOutputDir(int fromYear)  => StockDataAnalysis.dataOutputDir + $"Shouban/{fromYear}_{System.DateTime.Now.Year}/";
    //public static string shoubanOutputPath(int fromYear, string stockCode) => shoubanOutputDir(fromYear) + $"{stockCode}.json";
    public int fromYear => AppController.instance.StockDataAnalysis.fromYear;
    
    protected override void onCompleted()
    {
        if (m_pickedStocks != null)
        {
            string content = JsonConvert.SerializeObject(m_pickedStocks);
            Debug.LogError($"选出的一字追妖股票有：{content}");
            onEnd?.Invoke(content);
        }
    }
    StockData FindYizizhuiyao(StockAllData stock)
    {
        Debug.Log($"FindYijianfenghou:{stock.stockData.dm}");

        var stockAllData = StocksDataController.instance.GetStockAllData(stock.stockData.dm, fromYear);
        if (stockAllData == null || stockAllData.stockTransitions == null)
            return null;

        var yijianfenghou = new StockTransitionsRanges();
        yijianfenghou.stockCode = stock.stockData.dm;
        yijianfenghou.transitionsRanges = new List<TransitionsRange>();
        
        var transitions = stockAllData.stockTransitions;

        if (transitions.Count < 5)
            return null;

        var cur = transitions[^1];
        var pre = transitions[^2];
        var qian = transitions[^3];
        var qian2 = transitions[^4];
        var qian3 = transitions[^5];
        
        //涨停或者上涨5%左右
        double goUpRate = (cur.c - pre.c) / pre.c;
        bool goUpTop = goUpRate> 0.096f;
        bool goUp5 = goUpRate >= 0.04 && goUpRate <= 0.06;
        if (!goUpTop && !goUp5)
        {
            return null;
        }
        
        //若涨停，需开盘就涨停
        if (goUpTop)
        {
            if (cur.o < cur.c - 0.01)
            {
                return null;
            }
        }
        
        //前一天开盘涨停
        bool preGoUpTop = (pre.c - qian.c) / qian.c > 0.096f;
        if (!preGoUpTop)
        {
            return null;
        }
        if (pre.o < pre.c - 0.01)
        {
            return null;
        }
        
        //3天前不是涨停板
        bool preGoUpTop2 = (qian2.c - qian3.c) / qian3.c > 0.096f;
        if (preGoUpTop2)
        {
            return null;
        }

        //Debug.Log($"选出股");
        return stock.stockData;
    }
    

    [ContextMenu("选一字追妖股")]
    public void PickAllYizizhuiyao()
    {
        actionInterval = 0.01f;
        m_pickedStocks = new List<StockData>();
        DoActionToAllStocks(DoPickYizizhuiyao);
    }

    private List<StockData> m_pickedStocks;
    async void DoPickYizizhuiyao(StockData stock)
    {
        var stockAllData = StocksDataController.instance.GetStockAllData(stock.dm,fromYear);
        if (stockAllData == null || stockAllData.stockTransitions == null)
            return;
        
        var shouban =  FindYizizhuiyao(stockAllData);
        if (shouban != null)
        {
            Debug.LogError($"选出一字追妖股：{stock.dm} ，波段：{JsonConvert.SerializeObject(shouban)}");
            m_pickedStocks.Add(stock);
        }
    }
}

